package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {
  
  @Autowired
  private CategoryService categoryService;
  
  /**
   * 新增分类
   * @param categoryDTO
   */
  @PostMapping
  public Result add(@RequestBody CategoryDTO categoryDTO) {
    log.info("新增分类：{}", categoryDTO);
    categoryService.add(categoryDTO);
    return Result.success();
  }
  
  /**
   * 分类分页查询
   * @param categoryDTO
   */
  @GetMapping("/page")
  public Result<PageResult> page(CategoryPageQueryDTO categoryDTO) {
    PageResult page = categoryService.page(categoryDTO);
    return Result.success(page);
  }
  
  /**
   * 根据id查询分类  x
   * @param id
   */
  public Result<Category> findById(@PathVariable Long id) {
    return Result.success();
  }
  
  /**
   * 修改分类
   * @param categoryDTO
   */
  @PutMapping
  public Result<String> update(@RequestBody CategoryDTO categoryDTO) {
    categoryService.update(categoryDTO);
    return Result.success();
  }
  
  /**
   * 根据类型查询分类
   * @param type
   */
  @GetMapping("/list")
  public Result<List<Category>> findByType(@RequestParam(required = false) String type) {
    List<Category> category = categoryService.findByType(type);
    return Result.success(category);
  }
  
  /**
   * 根据id删除分类
   * @param id
   */
  @DeleteMapping()
  public Result<?> delete(@RequestParam Long id) {
    categoryService.del(id);
    return Result.success();
  }
  
  /**
   * 启用禁用分类
   * status: 1为启用 0为禁用
   */
  @PostMapping("/status/{status}")
  public Result<?> changeStatus(@PathVariable Integer status, @RequestParam Long id) {
    categoryService.changeStatus(status, id);
    return Result.success();
  }
  
  
  
}
